package com.blinddate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blinddate.pojo.entity.GoodsType;
import com.blinddate.pojo.result.Result;
import com.blinddate.service.GoodsTypeService;
import com.blinddate.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/goods")
@Slf4j
@Api(tags = "商品信息相关")
public class GoodsController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private UserService userService;

    @GetMapping("/get_goods_inf")
    @ApiOperation("获取所用商品信息")
    public Result<List<GoodsType>> getGoodsInf(@RequestParam Integer page,@RequestParam Integer size){
        //判断用户权限
        Boolean decide = userService.identityDecide();
        if (!decide){
            return Result.error("用户权限不够");
        }

        IPage<GoodsType> iPage = new Page<>(page,size);
        goodsTypeService.page(iPage);
        List<GoodsType> list = iPage.getRecords();
        if (list == null ){
            return Result.error("商品信息获取失败");
        }
        return Result.success(list);
    }


    @PostMapping("/update_goods_inf")
    @ApiOperation("更新商品信息")
    public Result<String> updateGoodsInf(@RequestBody GoodsType goodsType){
        //判断用户权限
        Boolean decide = userService.superAdmin();
        if (!decide){
            return Result.error("用户权限不够");
        }

        log.info("进入");
        GoodsType goodsType1 = goodsTypeService.getById(goodsType.getId());
        if (goodsType1==null){
            return Result.error("没有该商品信息,无法进行更新");
        }

        boolean b = goodsTypeService.updateById(goodsType);
        if (b){
            return Result.success("商品信息修改成功");
        }
        return Result.error("商品信息修改失败");
    }

    @GetMapping("/get_one_goods")
    @ApiOperation("获得单个商品信息")
    public Result<GoodsType> getOneGoods(@RequestParam Long goodsId){
        //判断用户权限
        Boolean decide = userService.identityDecide();
        if (!decide){
            return Result.error("用户权限不够");
        }

        GoodsType goodsType = goodsTypeService.getById(goodsId);
        if (goodsType!=null){
            return Result.success(goodsType);
        }
        return Result.error("获取商品信息失败");
    }

    @PostMapping("/insert_goods")
    @ApiOperation("上传商品信息")
    public Result<String> insertGoods(@RequestBody GoodsType goodsType){
        //判断用户权限
        Boolean decide = userService.superAdmin();
        if (!decide){
            return Result.error("用户权限不够");
        }

        boolean save = goodsTypeService.save(goodsType);
        if (save){
            return Result.success("商品上传成功");
        }
        return Result.error("商品上传失败");
    }

    @DeleteMapping("/delete_goods")
    @ApiOperation("删除商品信息")
    public Result<String> deleteGoods(Long goodsId){
        //判断用户权限
        Boolean decide = userService.superAdmin();
        if (!decide){
            return Result.error("用户权限不够");
        }

        boolean b = goodsTypeService.removeById(goodsId);
        if (b){
            return Result.success("商品删除成功");
        }
        return Result.error("商品删除失败");
    }

    @GetMapping("/type_goods")
    @ApiOperation("获取指定类型的商品")
    public Result<List<GoodsType>> typeGoods(@RequestParam Integer type,@RequestParam Integer page,@RequestParam Integer size){
        //判断用户权限
        Boolean decide = userService.identityDecide();
        if (!decide){
            return Result.error("用户权限不够");
        }

        LambdaQueryWrapper<GoodsType> goodsTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        IPage<GoodsType> iPage = new Page<>(page,size);
        goodsTypeLambdaQueryWrapper.eq(GoodsType::getType,type);

        goodsTypeService.page(iPage,goodsTypeLambdaQueryWrapper);
        List<GoodsType> list = iPage.getRecords();
        if (list == null ){
            return Result.error("商品信息获取失败");
        }
        return Result.success(list);
    }

    @GetMapping("get_goods_use")
    @ApiOperation("获取商品列表给用户")
    public Result<List<GoodsType>> getGoodsUse(@RequestParam Integer type){
        //通过类型进行返回
        LambdaQueryWrapper<GoodsType> goodsTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsTypeLambdaQueryWrapper.eq(GoodsType::getType,type);
        List<GoodsType> list = goodsTypeService.list(goodsTypeLambdaQueryWrapper);
        if (list == null ){
            return Result.error("商品信息获取失败");
        }
        return Result.success(list);
    }

}
